/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of Game packet.
 *
 * Game packet is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * Game packet is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Game packet.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.game.selection;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;

import fi.honkalampisaatio.game.ui.ScalableIconButton;

public class SelectionButton extends ScalableIconButton {

	private static final long serialVersionUID = 941039050582813245L;
	
	private String user = null;

	public SelectionButton(String name, String imagePath) {
		super(name, imagePath);
	}

	public SelectionButton(String name) {
		super(name);
	}

	public void setUser(String user) {
		this.user = user;
		this.repaint();
	}

	public void paintComponent(Graphics g) {
		super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
		if(user!=null) {
			double scale = 1.0;
			float fontSize = 144;
        	g2.setFont(getFont().deriveFont(144));
        	Rectangle2D rc = g2.getFontMetrics().getStringBounds(this.user, g2);
    		scale = this.getWidth()*0.9/rc.getWidth();
    		fontSize = (int)(fontSize*scale);
        	g2.setFont(getFont().deriveFont(fontSize));
        	rc = g2.getFontMetrics().getStringBounds(this.user, g2);
        	if(rc.getHeight()>this.getHeight()*0.9) {
        		scale = this.getHeight()*0.9/rc.getHeight();
        		fontSize = (int)(fontSize*scale);
            	g2.setFont(getFont().deriveFont(fontSize));
            	rc = g2.getFontMetrics().getStringBounds(this.user, g2);
        	}
        	int y = (int)((this.getHeight()+rc.getHeight())/2-g2.getFontMetrics().getDescent());
        	int x = (int)((this.getWidth()-rc.getWidth())/2);
        	g2.drawString(this.user, x, y);
		}
	}

}
